package com.hz.web.controller;

import com.hz.web.entity.*;
import com.hz.web.service.ICertificatoryCampanyService;
import com.hz.web.service.ICompanyBaseService;
import com.hz.web.service.ICompanyEmployeeStatusService;
import com.hz.web.service.ICompanyLevelInfoService;
import com.hz.web.util.IdCreator;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("companyBase")
public class CompanyBaseInfoController {

    @Resource
    private ICompanyBaseService companyBaseService;
    @Resource
    private ICompanyEmployeeStatusService companyEmployeeStatusService;
    @Resource
    private ICompanyLevelInfoService companyLevelInfoService;

    @Resource
    private ICertificatoryCampanyService certificatoryCampanyService;

    /**
     * 进入基本信息页面
     *
     * @return
     */
    @RequestMapping("baseInfo")
    public String toBaseInfohtml(HttpSession session, Model model,String userId) {
        if(null== userId){
            UserEntity entity = (UserEntity) session.getAttribute("userinfo");
            userId = entity.getId()+"";
        }

        CompanyBaseInfo companyBaseInfo = companyBaseService.selectByUserId(Integer.valueOf(userId));
        CompanyEmployeeStatus companyEmployeeStatus = companyEmployeeStatusService.selectByCompanyId(companyBaseInfo.getId());
        CompanyLevelInfo companyLevelInfo = companyLevelInfoService.selectByCompanyId(companyBaseInfo.getId());
        Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
        parmsMap.put("companyId", Integer.valueOf(userId));
        List<CertificatoryComp> certificatoryCompList = certificatoryCampanyService.selectByCompanyId(parmsMap);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        if (null != certificatoryCompList && certificatoryCompList.size() > 0) {
            for (CertificatoryComp certificatoryComp : certificatoryCompList) {
                if (certificatoryComp.getCertificateValidity() != null) {
                    certificatoryComp.setCertificateValidityStr(sdf.format(certificatoryComp.getCertificateValidity()));
                }
            }
        }
        model.addAttribute("certificatoryCompList", certificatoryCompList);

        if (companyEmployeeStatus == null) {
            companyEmployeeStatus = new CompanyEmployeeStatus();
            companyEmployeeStatus.setCompanyId(companyBaseInfo.getId());
        }
        if (companyLevelInfo == null) {
            companyLevelInfo = new CompanyLevelInfo();
            companyLevelInfo.setCompanyId(companyBaseInfo.getId());
        }

        companyBaseInfo.setCompanyEmployeeStatus(companyEmployeeStatus);
        companyBaseInfo.setCompanyLevelInfo(companyLevelInfo);

        model.addAttribute("companyBaseInfo", companyBaseInfo);
        return "company_base_manage";
    }


    /**
     * 新增
     *
     * @return
     */
    @RequestMapping("insertBaseInfo")
    @ResponseBody
    public Map<String, Object> insertEmployee(HttpSession session, CompanyBaseInfo entity) {
        Map<String, Object> resultMap = new HashMap<>();//结果map
        int row = -1;
        String name = "";
        if (entity.getName() != null) {
            name = entity.getName().replaceAll(" ", "");
            Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
            parmsMap.put("name", name);
            List<CompanyBaseInfo> list = companyBaseService.queryList(parmsMap);
            if (list.size() > 0 && entity.getId() == null) {
                resultMap.put("row", row);
                resultMap.put("msg", "此企业信息已存在！");
                return resultMap;
            }
        } else {
            resultMap.put("row", row);
            resultMap.put("msg", "企业名称为必填字段！");
            return resultMap;
        }

        CompanyEmployeeStatus companyEmployeeStatus = entity.getCompanyEmployeeStatus();

        if (entity.getId() != null) {
            // update
            entity.setModifyTime(new Date());
            companyBaseService.updateById(entity);

            CompanyEmployeeStatus oldCompanyEmployeeStatus = companyEmployeeStatusService.selectByCompanyId(entity.getId());
            companyEmployeeStatus.setId(oldCompanyEmployeeStatus.getId());
            companyEmployeeStatus.setModifyTime(new Date());
            companyEmployeeStatusService.updateById(companyEmployeeStatus);

            resultMap.put("companyBaseId", entity.getId());
            resultMap.put("companyId", entity.getId());
            resultMap.put("row", row);
            resultMap.put("msg", "更新成功！");
        } else {
            // add
            UserEntity userEntity = (UserEntity) session.getAttribute("userinfo");

            entity.setId(IdCreator.getId());
            entity.setUserId(userEntity.getId());
            entity.setName(name);
            entity.setCreateTime(new Date());
            entity.setModifyTime(new Date());

            companyEmployeeStatus.setId(IdCreator.getId());
            companyEmployeeStatus.setCompanyId(entity.getId());
            companyEmployeeStatus.setCreateTime(new Date());
            companyEmployeeStatus.setModifyTime(new Date());

            try {
                row = companyBaseService.insert(entity);
                companyEmployeeStatusService.insert(companyEmployeeStatus);

            } catch (Exception e) {
                e.printStackTrace();
                resultMap.put("row", row);
                resultMap.put("msg", "添加异常");
                return resultMap;
            }
            resultMap.put("companyBaseId", entity.getId());
            resultMap.put("companyId", entity.getId());
            resultMap.put("row", row);
            resultMap.put("msg", "添加成功！");
        }

        return resultMap;
    }


    /**
     * 新增资质等级
     *
     * @return
     */
    @RequestMapping("insertLevelInfo")
    @ResponseBody
    public Map<String, Object> insertEmployee(HttpSession session, CompanyLevelInfo entity) {
        Map<String, Object> resultMap = new HashMap<>();//结果map
        int row = -1;

        if (entity.getId() != null) {
            // update
            entity.setModifyTime(new Date());
            companyLevelInfoService.updateById(entity);

            resultMap.put("companyLevelInfoId", entity.getId());
            resultMap.put("row", row);
            resultMap.put("msg", "更新成功！");
        } else {
            // add
            entity.setId(IdCreator.getId());
            entity.setCreateTime(new Date());
            entity.setModifyTime(new Date());

            try {
                row = companyLevelInfoService.insert(entity);

            } catch (Exception e) {
                e.printStackTrace();
                resultMap.put("row", row);
                resultMap.put("msg", "添加异常");
                return resultMap;
            }
            resultMap.put("companyLevelInfoId", entity.getId());
            resultMap.put("row", row);
            resultMap.put("msg", "添加成功！");
        }

        return resultMap;
    }

    @InitBinder
    public void InitBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(
                dateFormat, true));
    }
}
